package com.jing.cloud.service.buddy.method;

import com.google.common.collect.Maps;
import com.jing.cloud.service.buddy.bean.Buddy;
import com.jing.cloud.service.buddy.dao.BuddyDao;
import com.jing.cloud.service.buddy.method.param.TsBean;
import com.jing.cloud.service.method.AbstractMethod;
import com.jing.cloud.service.method.Method;
import com.jing.cloud.service.util.db.Compare;
import com.jing.cloud.service.util.db.OrderBy;
import com.jing.cloud.service.util.db.Page;
import com.jing.cloud.service.Res;
import com.jing.cloud.util.RspUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Created by 29017 on 2017/9/20.
 */
@Component
public class GetBuddies extends AbstractMethod<TsBean> implements Method<TsBean> {

    @Autowired
    private BuddyDao buddyDao;

    @Override
    public Res call(TsBean tsBean) throws Exception {
        long userId = getUserId();
        long ts = tsBean.getTs();
        Map<String, Object> condition = Maps.newHashMap();
        condition.put("userId", userId);
        Compare cUpdatedAt = new Compare();
        cUpdatedAt.setGt(ts);
        condition.put("updatedAt", cUpdatedAt);
        Page<Buddy> page = new Page<>();
        page.setPage(1);
        page.setPageSize(tsBean.getSize());
        page.addOrderBy(new OrderBy("updatedAt", true));
        page = buddyDao.query(condition, page);
        return RspUtil.success(page);
    }
}
